package com.ewins.weatherdataclient.https;

import com.ewins.weatherdataclient.common.Constant;

import javax.net.ssl.*;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.PasswordAuthentication;

/**
 * Created by hp on 2015/12/10.
 */
public class HttpsDefault {
    public static void setDefaultValue()
    {
        HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
        Authenticator.setDefault(new MyAuthenticator (Constant.UserName, Constant.UserPassword));
    }

    static SSLSocketFactory sslFactory = null;
    public static SSLSocketFactory getSSLSocketFactory() throws Exception
    {
        if(sslFactory == null) {
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, tm, new java.security.SecureRandom());
            sslFactory = sslContext.getSocketFactory();
        }
        return sslFactory;
    }

    /**
     * 忽视证书HostName
     */
    private static HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {
        public boolean verify(String s, SSLSession sslsession) {

//            System.out.println("WARNING: Hostname is not matched for cert.");
            return true;
        }
    };

}
